package com.fenghongzhang.day10matrix;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

public class MyView extends View {
    Matrix matrix;
    Paint paint;
    Bitmap mBitmap;
    public MyView(Context context) {
        super(context);
        init();
    }

    public MyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
         matrix = new Matrix();
         paint = new Paint(Paint.ANTI_ALIAS_FLAG);
         mBitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawBitmap(mBitmap,matrix,paint);
    }

    public void scale(float x,float y){
//        matrix.setScale(x,y);
        matrix.preScale(x,y);
//        matrix.postScale(x,y);
        invalidate();
    }

    public void setTranslate(float x,float y){
//        matrix.setTranslate(x,y);
        matrix.preTranslate(x,y);
        invalidate();
    }

    public void setTotate(int d,float x,float y){
        matrix.setRotate(d,x,y);
        invalidate();
    }

    public void setSkew(float x,float y){
        matrix.setSkew(x,y);
        invalidate();
    }

    public void mark(){
        Bitmap bitmap = Bitmap.createBitmap(mBitmap.getWidth(), mBitmap.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        canvas.drawBitmap(mBitmap,0,0,paint);
        canvas.drawText("shuai.com",20,20,paint);
        mBitmap = bitmap;
        invalidate();
    }
}
